import math
import numpy
import pylab
import csv


class WetNeuron:
    '''
    represents a cell we loaded with dye and recorded.
    knows its index number and its parent slice, 
    so it's spike-record is stored in the movies of its parent-slice at that index.
    also knows its contours for plotting.
    '''


    def __init__(self, index, parentSlice, directory, contourFilePre, contourFilePost):
	'''
	the contour file is found by concatenating pre, index, and post. 
	'''
	self.index = index
	self.slice = parentSlice
	#actualIndex = index - 1
	filename = ''.join([directory, contourFilePre, `(index+1)`, contourFilePost])
	contoursfile = csv.reader(open(filename, 'rb'))
	self.ID = ''.join([parentSlice.ID, filename])

	rawData = [row for row in contoursfile] #this isn't quite the format we want
	lessRaw = numpy.array(rawData) #this is closer but it's still one big array
	almostRipe = numpy.split(lessRaw, 2, axis=1) #this splits lessRaw into its 2 columns-- like matlab (:,1) 
	#however, almostRipe is now an array that contains an array for each value, and the little sub-arrays are string-types instead of floating-valued types!
	#this is how to store for easy plotting later:
	self.Xvals = map(lambda x: float(x[0]), almostRipe[0])
	self.Yvals = map(lambda x: float(x[0]), almostRipe[1])
	
	
    def plot(self, axes=None, filled=False, colorString=None):
	'''
	plots the current neuron either on a figure you pass in or just on its own canvas
	@param axes: the way to do this like: g = pylab.figure() ; axes = g.gca(); then this neuron can plot itself to those axes.
	@param colorString: lets you set the color
    
	'''
	if(colorString == None):
	    if(not filled):
		colorString = 'k'
	    else:
		colorString = 'm'

	showit = False
	if(axes == None):
	    fig = pylab.figure()
	    axes = fig.gca()
	    showit = True

	if(filled):
	    axes.plot(self.Xvals, self.Yvals, colorString)
	    axes.fill(self.Xvals, self.Yvals, colorString)
	else:
	    axes.plot(self.Xvals, self.Yvals, colorString)
	
	if(showit):
	    axes.show()




	
